package com.ledim.activity;

import android.annotation.TargetApi;
import android.content.ClipboardManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ledim.activity.base.LedimBaseListActivity;
import com.ledim.adapter.ContributeAdapter;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.ContributeListBean;
import com.ledim.bean.base.BaseListResponse;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.LogUtil;
import com.ledim.utils.ToastUtil;
import com.letv.android.young.client.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 投稿界面
 */
public class UserContributeActivity
        extends LedimBaseListActivity<ContributeListBean>
{

	private ImageView mBack;
	private ImageView mCopyAddress;
	private ClipboardManager myClipboard;
	private View mBottomUploadView;

	@Override
	protected int getContentLayoutId()
	{
		return R.layout.activity_user_contribute;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		initView();
		getWindow().setBackgroundDrawable(null);

	}

	@Override
	protected LedimBaseListAdapter getAdapter()
	{
		return new ContributeAdapter(this);
	}

	@Override
	protected boolean enablePullRefresh()
	{
		return false;
	}

	@Override
	protected void getDatas()
	{
		LedimApiServiceFactory.createService(ApiService.class)
		        .getContributeList(Integer.valueOf(LedimSpUtils.getUid()),
		                mLoadPage + 1, getPageSize())
		        .observeOn(AndroidSchedulers.mainThread())
		        .subscribeOn(Schedulers.io()).subscribe(
		                new LedimHttpObserver<BaseListResponse<ContributeListBean>>()
		                {

			                @Override
			                public void onCompleted()
			                {
				                super.onCompleted();

			                }

			                @Override
			                public void onError(Throwable e)
			                {
				                super.onError(e);
				                LogUtil.e(e.getMessage());
				                setServerDataError();
				                setUploadViewState(false);
				                setDefaultImgLpParams();
			                }

			                @Override
			                public void onNext(
			                        BaseListResponse<ContributeListBean> dataInfo)
			                {
				                super.onNext(dataInfo);
				                boolean hasData = false;
				                if (dataInfo != null && dataInfo.success)
				                {
					                initTotalPages(dataInfo.paged.total);
					                setDatas(dataInfo.data,
					                        dataInfo.paged.more);
					                if (dataInfo.data != null
					                        && dataInfo.data.size() > 0)
					                {
						                mCopyAddress
						                        .setVisibility(View.VISIBLE);
						                hasData = true;
					                }
				                }
				                else
				                {
					                setServerDataError();
				                }
				                setUploadViewState(hasData);
				                setDefaultImgLpParams();
			                }
		                });

	}

	/**
	 * 设置默认图片的LayoutParams
	 */
	private void setDefaultImgLpParams()
	{
		LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mLedimStateView
		        .getLayoutParams();
		int bottomMargin = DensityUtil.dip2px(this, 175);

		if (layoutParams.bottomMargin !=  bottomMargin){
            layoutParams.bottomMargin = bottomMargin;
        }
	}

	/**
	 * @param hasData
	 *            设置底部上传视频view
	 */
	private void setUploadViewState(boolean hasData)
	{

		if (hasData)
		{
			if (mBottomUploadView != null)
			{
				mBottomUploadView.setVisibility(View.GONE);
			}
		}
		else
		{
			if (mBottomUploadView == null)
			{
				ViewStub mViewStub = (ViewStub) findViewById(
				        R.id.vs_nodata_layout);
				mBottomUploadView = mViewStub.inflate();
				copyAddress((ImageView) mBottomUploadView);
			}
			mBottomUploadView.setVisibility(View.VISIBLE);
		}
	}

	private void initView()
	{
		mBack = findViewByIdExt(R.id.iv_back);
		mBack.setVisibility(View.VISIBLE);
		mBack.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		((TextView) findViewByIdExt(R.id.tv_title)).setText("我的投稿");

		mCopyAddress = this.findViewByIdExt(R.id.iv_copy_address);
		myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
		copyAddress(mCopyAddress);
	}

	/**
	 * @param mImageView
	 */
	private void copyAddress(ImageView mImageView)
	{
		mImageView.setOnLongClickListener(new View.OnLongClickListener()
		{
			@TargetApi(Build.VERSION_CODES.HONEYCOMB)
			@Override
			public boolean onLongClick(View v)
			{
				myClipboard.setText(
				        getResources().getString(R.string.contribute_address));
				ToastUtil.toastCenter("复制成功!");
				return false;
			}
		});
	}

	@Override
	public int getNoDataStateTextId()
	{
		return R.string.contribut_nodata_des;
	}
}
